package com.doctor.web.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.doctor.common.config.properties.OssConfigProperties;
import com.doctor.common.core.domain.entity.SysUser;
import com.doctor.common.exception.ServiceException;
import com.doctor.common.exception.file.FileUploadException;
import com.doctor.common.utils.DateUtils;
import com.doctor.common.utils.FileUtil;
import com.doctor.system.mapper.SysUserMapper;
import com.doctor.web.domain.vo.CommentsVo;
import com.doctor.web.domain.vo.CreateByVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.doctor.web.mapper.ActivityMapper;
import com.doctor.web.domain.Activity;
import com.doctor.web.service.IActivityService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class ActivityServiceImpl implements IActivityService 
{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private OssConfigProperties ossConfigProperties;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询活动管理
     * 
     * @param id 活动管理主键
     * @return 活动管理
     */
    @Override
    public Activity selectActivityById(Long id)
    {
        Activity activity = activityMapper.selectActivityById(id);

        return activity;
    }

    /**
     * 查询活动管理列表
     * 
     * @param activity 活动管理
     * @return 活动管理
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        return activityMapper.selectActivityList(activity);
    }

    /**
     * 新增活动管理
     * 
     * @param activity 活动管理
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity)
    {
        activity.setCreateTime(DateUtils.getNowDate());
        return activityMapper.insertActivity(activity);
    }

    /**
     * 修改活动管理
     * 
     * @param activity 活动管理
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity)
    {
        activity.setUpdateTime(DateUtils.getNowDate());
        return activityMapper.updateActivity(activity);
    }

    /**
     * 批量删除活动管理
     * 
     * @param ids 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteActivityByIds(Long[] ids)
    {
        return activityMapper.deleteActivityByIds(ids);
    }

    /**
     * 删除活动管理信息
     * 
     * @param id 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteActivityById(Long id)
    {
        return activityMapper.deleteActivityById(id);
    }

    @Override
    public List<Activity> selectLatestActivities() {
        return activityMapper.selectLatestActivities();
    }

    @Override
    public List<Activity> selectActivitiesByDateRange(Date startDate, Date endDate) {
        return activityMapper.selectActivitiesByDateRange(startDate, endDate);
    }

    @Override
    public List<Activity> selectActivityListByCreateBy(String createBy) {
        return activityMapper.selectActivityListByCreateBy(createBy);
    }

    @Override
    public String saveImages(MultipartFile file) {
        try {
            String md5 = FileUtil.getMd5(file.getInputStream());
            String extName = FileUtil.getExtName(file.getOriginalFilename());
            String fileName = md5 + extName;
            if (!exists(fileName)) {
                upload(fileName, file.getInputStream());
            }
            return getFileAccessUrl("music/activity/" + fileName);
        } catch (Exception e) {
            throw new ServiceException("文件上传失败");
        }
    }

    public Boolean exists(String filePath) {
        return getOssClient().doesObjectExist(ossConfigProperties.getBucketName(), filePath);
    }

    public void upload(String fileName, InputStream inputStream) {
        getOssClient().putObject(ossConfigProperties.getBucketName(), "music/activity/" + fileName, inputStream);
    }

    private OSS getOssClient() {
        return new OSSClientBuilder().build(ossConfigProperties.getEndpoint(), ossConfigProperties.getAccessKeyId(), ossConfigProperties.getAccessKeySecret());
    }

    public String getFileAccessUrl(String filePath) {
        return ossConfigProperties.getUrl() + filePath;
    }

    @Override
    public CreateByVo getUserInfo(Long id) {
        Activity activity = activityMapper.selectActivityById(id);
        String createBy = activity.getCreateBy();
        Long createUserId = Long.valueOf(createBy);

        SysUser sysUser = sysUserMapper.selectUserById(createUserId);

        CreateByVo createByVo = new CreateByVo();
        createByVo.setAvatar(sysUser.getAvatar());
        createByVo.setUserId(sysUser.getUserId());
        createByVo.setNickName(sysUser.getNickName());

        return createByVo;
    }
}
